package cn.jinjimi.app.store.dao;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import cn.jinjimi.base.util.GsonUtils;
import cn.jinjimi.app.AccountManager;
import cn.jinjimi.app.store.entity.CacheData;

import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.AbstractDaoSession;
import de.greenrobot.dao.Property;
import de.greenrobot.dao.internal.DaoConfig;

/**
 * 网络请求数据缓存表
 * <p/>
 * Created by Jinsen on 16/1/20.
 */
public class CacheDao extends AbstractDao<CacheData, String> {

    public static final String TABLENAME = "net_cache";

    public static class Properties {
        public static final Property Id = new Property(0, String.class, "id", true, "_id");
        public static final Property Ssoid = new Property(1, String.class, "ssoid", false, "SSOID");
        public static final Property Data = new Property(2, String.class, "data", false, "DATA");
        public static final Property Time = new Property(3, Long.class, "time", false, "TIME");

        public Properties() {
        }
    }

    public static void createTable(SQLiteDatabase db, boolean ifNotExists) {
        if (db == null) return;
        String constraint = ifNotExists ? "IF NOT EXISTS " : "";
        db.execSQL("CREATE TABLE " + constraint + "\'" + TABLENAME + "\' ("
                + "\'" + Properties.Id.columnName + "\' TEXT PRIMARY KEY ,"
                + "\'" + Properties.Ssoid.columnName + "\' TEXT,"
                + "\'" + Properties.Data.columnName + "\' TEXT,"
                + "\'" + Properties.Time.columnName + "\' INTEGER);");
    }

    public static void dropTable(SQLiteDatabase db, boolean ifExists) {
        if (db == null) return;
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\'" + TABLENAME + "\'";
        db.execSQL(sql);
    }

    public CacheDao(DaoConfig config) {
        super(config);
    }

    public CacheDao(DaoConfig config, AbstractDaoSession daoSession) {
        super(config, daoSession);
    }

    @Override
    protected CacheData readEntity(Cursor cursor, int offset) {
        CacheData cacheData = new CacheData();
        cacheData.setId(cursor.getString(0 + offset));
        cacheData.setSsoid(cursor.getString(1 + offset));
        cacheData.setData(cursor.getString(2 + offset));
        cacheData.setTime(cursor.getLong(3 + offset));
        return cacheData;
    }

    @Override
    protected String readKey(Cursor cursor, int offset) {
        return cursor.getString(0 + offset);
    }

    @Override
    protected void readEntity(Cursor cursor, CacheData entity, int offset) {
        entity.setId(cursor.getString(0 + offset));
        entity.setSsoid(cursor.getString(1 + offset));
        entity.setData(cursor.getString(2 + offset));
        entity.setTime(cursor.getLong(3 + offset));
    }

    @Override
    protected void bindValues(SQLiteStatement stmt, CacheData entity) {
        stmt.clearBindings();
        String id = entity.getId();
        if (id != null) {
            stmt.bindString(1, id);
        }

        String ssoid = entity.getSsoid();
        if (ssoid != null) {
            stmt.bindString(2, ssoid);
        }

        String data = entity.getData();
        if (data != null) {
            stmt.bindString(3, data);
        }

        long time = entity.getTime();
        if (time > 0) {
            stmt.bindLong(4, time);
        }
    }

    @Override
    protected String updateKeyAfterInsert(CacheData entity, long rowId) {
        return entity.getId();
    }

    @Override
    protected String getKey(CacheData entity) {
        return entity.getId();
    }

    @Override
    protected boolean isEntityUpdateable() {
        return true;
    }

    public void saveCacheData(Object data, String id) {
        saveCacheData(data, id, true);
    }

    public void saveCacheData(Object data, String id, boolean withLoginState) {
        if (data == null)
            return;

        CacheData cacheData = new CacheData();
        cacheData.setId(getNetCacheKey(id, withLoginState));
        cacheData.setData(GsonUtils.toJson(data));
        cacheData.setSsoid(withLoginState ? (AccountManager.getInstance().isLogin() ? AccountManager.getInstance().getAccount().ssoid : UNLOGINCACHE) : "");

        insertOrReplaceInTx(cacheData);
    }

    public String getCacheData(String id) {
        return getCacheData(id, true);
    }

    public String getCacheData(String id, boolean withLoginState) {
        CacheData data = load(getNetCacheKey(id, withLoginState));
        if (data != null)
            return data.getData();

        return null;
    }

    private String getNetCacheKey(String type, boolean withLoginState) {
        return type + (withLoginState ? (AccountManager.getInstance().isLogin() ? AccountManager.getInstance().getAccount().ssoid : UNLOGINCACHE) : "");
    }

    /* 我页面动态菜单 */
    public static final String MEDYNAMICMENU = "me_dynamic_menu";

    /* 存储文章分类数据 */
    public static final String READERCATEGOR = "reader_category_cache_";

    /* 赚多保常用产品 */
    public static final String ZDBPROCOMCACHE = "zdb_pro_com_cache";

    /* 赚多保焕新专场 */
    public static final String ZDBPRONEWCACHE = "zdb_pro_new_cache";

    /* 赚多保广告 */
    public static final String ZDBADVCACHE = "zdb_adv_cache";

    /* 赚多保商品缓存 */
    public static final String ZDBPRONORMALCACHE = "zdb_pro_normal_cache";

    /* 赚多保热销产品 */
    public static final String ZDBPROHOTSELLCACHE = "zdb_pro_hot_sell_cache";

    /* 首页广告 */
    public static final String HOMEADCACHE = "home_adv_cache";

    /* 首页公告 */
    public static final String HOMEDYNAMICCACHE = "home_adv_cache";

    /* 问吧广告 */
    public static final String WENBAHOMEADBCACHE = "wen_ba_adv_cache";

    /* 学习首页广告 */
    public static final String LEARNHOMEADVCACHE = "learn_home_adv_cache";

    /* 闪屏广告 */
    public static final String SPLASHSCREENADVCACHE = "splash_screen_adv_cache";

    /* 学习名师荟 */
    public static final String LEARNLECTURERFAMOUS = "learn_lecturer_famous_cache";

    /* 学习所有课程-仅缓存第一页 */
    public static final String LEARNALLCOURSE = "learn_all_course_cache";

    /* 所有讲师-仅缓存第一页 */
    public static final String LEARNALLLECTURERS = "learn_all_lecturers";

    /* 订阅讲师 */
    public static final String LEARNLECTURERSUBSCRIBE = "learn_subscribe_lecturer";

    /* 课程category缓存 */
    public static final String LEARNCATEGORYCACHE = "learn_category_cache";

    /* 课程category列表缓存 */
    public static final String LEARNCOURSECATEGORYCACHE = "learn_course_category_cache_";

    /* 课程收藏列表缓存 */
    public static final String LEARNCOURSECOLLECT = "learn_course_collect";

    /* 首页素材 */
    public static final String HOMEMATERIALCACHE = "home_material_cache";

    /* 首页计划书 */
    public static final String HOMEPLANCACHE = "home_plan_cache";

    public static final String UNLOGINCACHE = "UNLOGIN";

    /* 会员信息 */
    public static final String ME_MEMBER_INFO = "me_member_info";

    /* 问吧我的回答 */
    public static final String WENBA_MY_ANSWER_CACHE = "wenba_my_answer_cache";

    /* 学习推荐课程*/
    public static final String LEARNCOURSEHOT = "learn_course_hot_cache";

    /* 学习精选课程*/
    public static final String LEARNTOPCOURSE = "learn_top_course_cache";

    /* 置顶提问 */
    public static final String LEARNTOPQUESTION = "learn_top_question";

    /* 最新提问－仅缓存第一页 */
    public static final String LEARNLASTQUESTION = "learn_last_question";

    /* 精华提问－仅缓存第一页*/
    public static final String LEARNNICEQUESTION = "learn_nice_question";

    /* 所有免费／悬赏提问－仅缓存第一页*/
    public static final String LEARNALLQUESTION = "learn_all_question";

    /* 所有我的提问－仅缓存第一页*/
    public static final String LEARNMYQUESTION = "learn_my_question";

    /* 所有我的回答－仅缓存第一页*/
    public static final String LEARNMYANSWER = "learn_my_answer";

    /* 所有我的收藏－仅缓存第一页*/
    public static final String LEARNMYCOLLECTION = "learn_my_collection";

    /* 所有栏目－仅缓存第一页*/
    public static final String LEARNALLCOLUMNS = "learn_all_columns";

    /* 推荐栏目*/
    public static final String LEARNRECOMMENDCOLUMNS = "learn_recommend_columns";

    /* 赚多保推荐产品 */
//    public static final String ZDBPROHOTCACHE = "zdb_pro_hot_cache";

    /* 首页精彩动态 */
//    public static final String HOMEMOMENTHOTCACHE = "home_moment_hot_cache";

    /* 首页最新动态 */
//    public static final String HOMEMOMENTNEWCACHE = "home_moment_new_cache";

    /* 首页信息流 */
//    public static final String HOMEFEEDCACHE = "home_feed_cache";

    /* 存储红色提醒列表数据 */
//    public static final String REDREMINDLIST = "red_remind_list";

    /* 学习首页栏目*/
    public static final String LEARNTOPCOLUMNS = "learn_top_cloums_cache";

    /* 赚多宝搜索配置 */
    public static final String ZDB_SEARCH_CONFIG = "zdb_search_config_cache";
}
